import { message } from "ant-design-vue";
import path from "@/framework/util/path.ts";
import { showMessageAndPrint } from "@/framework/util/log.ts";

/**
 * 打开系统高级属性
 */
export function openSystemPropertiesAdvanced() {
  return native.cp.sudo("C:\\Windows\\System32\\SystemPropertiesAdvanced.exe");
}

/**
 * 创建符号链接
 * @param srcPath 源路径
 * @param linkPath 符号链接路径
 * @param show 显示操作提示
 */
export async function mklink(srcPath: string, linkPath: string, show = true) {
  try {
    if (await native.fs.exists(linkPath)) {
      await native.fs.rm(linkPath);
    }
    const dir = path.dirname(linkPath);
    if (!(await native.fs.exists(dir))) {
      await native.fs.mkdir(dir, { recursive: true });
    }
    await native.fs.symlink(srcPath, linkPath);
    if (show) {
      message.success("操作成功");
    }
  } catch (e) {
    if (show) {
      showMessageAndPrint(e);
    } else {
      console.log(e);
    }
  }
}

/**
 * 使用系统命令删除文件夹
 * @param dir 文件夹
 */
export async function rmdirViaCmd(dir: string) {
  try {
    await native.cp.execCmd(`cmd /c rmdir /s /q "${dir}"`);
  } catch (e) {
    console.error("delete pgsql version error", dir, e);
  }
}
